package com.coderbean.demo.leaveapply.listener;

import com.coderbean.demo.util.SpringContextUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.service.delegate.DelegateTask;

import com.coderbean.demo.leaveapply.entity.TLeaveApplyModel;
import com.coderbean.demo.leaveapply.service.ITLeaveApplyService;

public class StartListener implements TaskListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4388936007137126862L;

	@Override
	public void notify(DelegateTask delegateTask) {
		// 获取请假流程主键
		String processId = delegateTask.getProcessInstanceId();
		RuntimeService runtimeService = SpringContextUtils.getBean(RuntimeService.class);
		ProcessInstance processInstanc = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
		String leaveId = processInstanc.getBusinessKey();
		if (!org.springframework.util.StringUtils.isEmpty(leaveId)) {
			// 设置请假天数
			ITLeaveApplyService leaveApplyService = SpringContextUtils.getBean(ITLeaveApplyService.class);
			TLeaveApplyModel leaveApplyModel = leaveApplyService.getById(leaveId);
			Integer days = leaveApplyModel.getDays();
			// 设置请假天数参数
			runtimeService.setVariable(delegateTask.getExecutionId(), "days", days);
			// 更新审批状态
			leaveApplyModel.setApproveStatus(1);
			leaveApplyService.updateById(leaveApplyModel);
		}
	}

}
